//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"github.com/google/uuid"
)

// VulnerabilityID holds the schema definition for the VulnerabilityID entity.
type VulnerabilityID struct {
	ent.Schema
}

// Fields of the VulnerabilityID.
func (VulnerabilityID) Fields() []ent.Field {
	return []ent.Field{
		field.UUID("id", uuid.UUID{}).
			Default(getUUIDv7).
			Unique().
			Immutable(),
		field.String("vulnerability_id").Comment("ID of the vulnerability, one of OSV, GHSA, CVE, or custom"),
		field.String("type").NotEmpty().Comment("Type of vulnerability, one of OSV, GHSA, CVE, or custom"),
	}
}

// Edges of the VulnerabilityID.
func (VulnerabilityID) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("vuln_equal_vuln_a", VulnEqual.Type).Ref("vulnerability_a"),
		edge.From("vuln_equal_vuln_b", VulnEqual.Type).Ref("vulnerability_b"),
		edge.From("metadata", VulnerabilityMetadata.Type).Ref("vulnerability_id"),
		edge.From("certify_vuln", CertifyVuln.Type).Ref("vulnerability"),
		edge.From("vex", CertifyVex.Type).Ref("vulnerability"),
	}
}

// Indexes of the VulnerabilityID.
func (VulnerabilityID) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("vulnerability_id", "type").Unique(),
		index.Fields("type"), // speed up frequently run queries to check when vuln type is not novuln
	}
}
